<?php
namespace app\admin\controller;

use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class CreditTotal extends AdminBaseController{

    /**
     * @功能：根据用户统计收藏
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_total(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");
        $real = IRequest::get("real");

        //判断是否为用户搜索
        $credit_favorite_where['cf.is_delete'] = IsDelete::No;
        if ($user_id) $credit_favorite_where['cf.user_id'] = $user_id;
        if ($nick) $credit_favorite_where['us.nick'] = array("like","%{$nick}%");
        if ($phone) $credit_favorite_where['us.phone'] = array("like","%{$phone}%");
        if ($real) $credit_favorite_where['us.real'] = array("like","%{$real}%");

        //取得数据
        $credit_favorite_list = IDb::getInstance("credit_favorite as cf")
            ->setDbFiled("cf.user_id,us.nick,us.phone,us.real,count(*) as favorite_num")
            ->setDbJoin('user as us','us.user_id=cf.user_id')
            ->setDbWhere($credit_favorite_where)
            ->setDbGroup("cf.user_id")
            ->setDbOrder("favorite_num desc,cf.user_id")
            ->pag();
        if ($credit_favorite_list === false){
            error('获取统计收藏失败');
        }

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("real",$real);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡收藏记录
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_list(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $credit_favorite_where['is_delete'] = IsDelete::No;
        $credit_favorite_where['user_id'] = $user_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder("credit_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据信用卡统计收藏
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_total(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id");
        $credit_product_name = IRequest::get("credit_product_name");

        //判断是否为用户搜索
        $credit_favorite_where['is_delete'] = IsDelete::No;
        if(!empty($credit_product_id)){
            $credit_favorite_where['credit_product_id'] = $credit_product_id;
        }else {
            if (empty($credit_product_id) && !empty($credit_product_name)) {
                //查询条件
                $credit_product_where['credit_product_name'] = array("like", "%{$credit_product_name}%");

                //取得用户信息
                $credit_product_list = IDb::getInstance("credit_product")->setDbWhere($credit_product_where)->sel();
                if (!empty($credit_product_list) && is_array($credit_product_list)) {
                    $credit_favorite_where['credit_product_id'] = array("in", array_column($credit_product_list, "credit_product_id"));
                }
            }
        }

        //取得数据
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbFiled("credit_product_id,count(*) as favorite_num")
            ->setDbWhere($credit_favorite_where)
            ->setDbGroup("credit_product_id")
            ->setDbOrder("favorite_num desc,credit_product_id")
            ->pag();

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_product_name",$credit_product_name);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据信用卡收藏记录
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_list(){
        //接收参数
        $credit_product_id = IRequest::get("credit_product_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $credit_favorite_where['is_delete'] = IsDelete::No;
        $credit_favorite_where['credit_product_id'] = $credit_product_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder("credit_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }
}
